
package org

import (
    "dhfshop/models"
	"dhfshop/models/reserve"
	"strings"
	"errors"
	"time"
	"github.com/astaxie/beego/orm"
)

//商家组织
type ValuePartner struct {
	PartnerId     int64   `orm:"column(partner_id);pk" json:"partner_id" form:"partner_id"` //组织编号 
	Name          string  `orm:"column(name)" json:"name" form:"name"`                      //组织名称
	Desc          string  `orm:"column(desc)" json:"desc" form:"desc"`                      //组织描述
	Logo          string  `orm:"column(logo)" json:"logo" form:"logo"`                      //组织LOGO
	IndustryCode  int    `orm:"column(industry_code);default(1)" json:"industry_code" form:"industry_code"` //行业类别1电商类(默认);2餐饮类;3美容美发类;4便利店类
	PayWayCode    int     `orm:"column(pay_way_code)" json:"pay_way_code" form:"pay_way_code"`  //支付方式编码：1 未认证支付方式  2  认证支付方式
	Tel           string    `orm:"column(tel)" json:"tel" form:"tel"`                         //电话
	Address       string    `orm:"column(address)" json:"address" form:"address"`             //联系地址
	Activated     bool      `orm:"column(activated)" json:"activated" form:"activated"`     //是否激活
	OrderVersion  string    `orm:"column(order_version)" json:"order_version" form:"order_version"`//5定制版6旗舰版7基础版8官网版9体验版
	ServeStartAt  time.Time `orm:"null;type(datetime);column(serve_start_at)" json:"serve_start_at" form:"serve_start_at"` //订购开始时间
	ServeEndAt    time.Time `orm:"null;type(datetime);column(serve_end_at)" json:"serve_end_at" form:"serve_end_at"` //订购结束时间
	CreatorId     int64     `orm:"column(creator_id)" json:"creator_id" form:"creator_id"`             //创建者ID
	CreatedAt     time.Time `orm:"auto_now_add;column(created_at);type(datetime)" json:"-" form:"-"`   // 创建时间
	UpdatedAt     time.Time `orm:"auto_now;column(updated_at);type(datetime)" json:"-" form:"-"` 
	// 非数据库字段
	CreatedAtStr  string    `orm:"-" json:"created_at" form:"-"`
	UpdatedAtStr  string    `orm:"-" json:"updated_at" form:"-"`
}

// 产品线独立出来
type ProductLine struct {
	ProductLineId int64     `orm:"column(product_line_id);pk;auto" json:"product_line_id" form:"product_line_id"` //编号 
	PartnerId     int64     `orm:"column(partner_id)" json:"partner_id" form:"partner_id"` //分销商编号 
	CompanyId     int64       `orm:"column(company_id)" json:"company_id" form:"company_id"`    //供应商ID
    Value         int64     `orm:"column(value)" json:"value" form:"value"`  //
	CreatedAt     time.Time `orm:"auto_now_add;column(created_at);type(datetime)" json:"-" form:"-"`   /* 创建时间 */
	CreatedAtStr  string    `orm:"-" json:"created_at" form:"-"`
	UpdatedAt     time.Time `orm:"auto_now;column(updated_at);type(datetime)" json:"-" form:"-"` 
	UpdatedAtStr  string    `orm:"-" json:"updated_at" form:"-"`
}

func init() {
    orm.RegisterModel(new(ValuePartner))   
}

func AddPartner(m *ValuePartner) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}

func GetPartnerById(id int64) (v *ValuePartner, err error) {
	o := models.GetDefaultDB()
	v = &ValuePartner{PartnerId: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetAllPartners(query map[string]string, fields []string, order_by map[string]string, 
    offset int, limit int) ([]*ValuePartner, int64,error) {
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(ValuePartner))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		qs = qs.Filter(k, v)
	}
	cnt, err := qs.Count()
	// order by:
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, 0, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	qs = qs.OrderBy(sortFields...)
	var lists []*ValuePartner
	if _, err := qs.Limit(limit, offset).All(&lists, fields...); err == nil {
		return lists, cnt, nil
	}
	return nil, 0, err
}

func UpdatePartnerById(m *ValuePartner, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := ValuePartner{PartnerId: m.PartnerId}
	if err = o.Read(&v); err != nil {
		return err
	}else{
	    if _, err = o.Update(m, k...); err != nil {
		   return err
		}
	}
	return nil
}

func DeletePartner(id int64) (err error) {
	o := models.GetDefaultDB()
	v := ValuePartner{PartnerId: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}

func InitDefaultPartnerData(partnerId int64) (int64, error) {
    roleId, err := InitDefaultAdminRole(partnerId)
	if err != nil {
	   return 0,err
	}
	reserve.InitDefaultReserveOpt(partnerId)
	reserve.InitDefaultPeriodSTLs(partnerId)
	reserve.InitDefaultTimeslotSTLs(partnerId)
	return roleId, nil
}
